package com.smooth.site.sx.pc.jifen.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.common.utils.IntegerUtils;
import com.smooth.dao.gen.BizJifenLogMapper;
import com.smooth.dao.gen.BizStudentMapper;
import com.smooth.dao.gen.bean.BizJifenLogExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.pc.jifen.enums.JifenType;
import com.smooth.site.sx.timer.TimeConverter;

@Component
public class PcJifenService {
	Logger log = LoggerFactory.getLogger(PcJifenService.class);
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BizJifenLogMapper bizJifenLogMapper;
	@Autowired
	BizStudentMapper bizStudentMapper;
	@Autowired
	PcJifenLogService pcJifenLogService;
	@Transactional
	public synchronized void loginAddJifen(BizStudent bizStudent, Integer jifenAmount) {

		if (isAddLoginJifen(bizStudent.getId())) {
			addJifen(bizStudent,jifenAmount,JifenType.login);
			log.info("【" + bizStudent.getId() + "】已经添加积分，直接跳过");
			return;
		}

	}
	
	public void subJifen(Integer jifen,BizStudent bizStudent,String orderNo,String content) {
		addJifen(bizStudent,-jifen,JifenType.goodsSub,orderNo,content);
	}
	
	
	public void completeStuInfo(BizStudent bizStudent, Integer jifenAmount) {
		log.info("该学生信息全");
		if(isRewordJifen(bizStudent.getId())) {
			addJifen(bizStudent,jifenAmount,JifenType.wanshan);
		}else {
			log.info("已经发过奖励");
		}
	}
	
	public void addJifen(BizStudent bizStudent, Integer jifenAmount, JifenType jifenType,String orderNo,String content) {
		pcJifenLogService.insertJifenLog(bizStudent.getJifenAmount(), jifenAmount, jifenType, bizStudent.getId(), bizStudent.getStuName(),orderNo,content);

		BizStudent bizStudentRecord = new BizStudent();
		bizStudentRecord.setId(bizStudent.getId());
		int amount = IntegerUtils.add(jifenAmount, bizStudent.getJifenAmount());

		if(amount<0) {
			throw new SiteException(SiteCodeEnums.JIFEN_NOENOUGH,bizStudent.getTelNo()+"");
		}
		
		if(jifenAmount>0) {
			int amountAll = IntegerUtils.add(jifenAmount, bizStudent.getJifenAllAmount());
			bizStudentRecord.setJifenAllAmount(amountAll);
		}
		bizStudentRecord.setJifenAmount(amount);
		int optNum = bizStudentMapper.updateByPrimaryKeySelective(bizStudentRecord);

		if (optNum == 1) {
			log.info("【" + bizStudent.getId() + "】添加了积分【" + jifenAmount + "】");
		} else {
			throw new SiteException(SiteCodeEnums.DB_ERROR, bizStudent.getId() + "");
		}
	}
	@Transactional
	public void addJifen(BizStudent bizStudent, Integer jifenAmount, JifenType jifenType) {
		addJifen(bizStudent, jifenAmount, jifenType, null, null);
	}
	private boolean isRewordJifen(String uid) {
		BizJifenLogExample bizJifenLogExample = new BizJifenLogExample();
		BizJifenLogExample.Criteria criteria = bizJifenLogExample.createCriteria();
		criteria.andJifenTypeEqualTo(JifenType.reword.value);
		criteria.andUserIdEqualTo(uid);

		long count = bizJifenLogMapper.countByExample(bizJifenLogExample);
		if (count > 0) {
			log.info("【" + uid + "】已经添加积分，直接跳过");
			return false;
		}
		return true;
	}
	
	private boolean isAddLoginJifen(String uid) {
		BizJifenLogExample bizJifenLogExample = new BizJifenLogExample();
		BizJifenLogExample.Criteria criteria = bizJifenLogExample.createCriteria();
		criteria.andJifenTypeEqualTo(JifenType.login.value);
		criteria.andUserIdEqualTo(uid);
		Date cdate = TimeConverter.getCurDateDay(new Date());
		criteria.andCreateDateGreaterThanOrEqualTo(cdate);

		long count = bizJifenLogMapper.countByExample(bizJifenLogExample);
		if (count > 0) {
			log.info("【" + uid + "】已经添加积分，直接跳过");
			return false;
		}
		return true;
	}
	
}
